<?php
class Agitum_Model_Privilege extends Agitum_Model_Object
{
    protected $_t_class = 'Agitum_Model_DbTable_Privilege';
    
    public static function getPrivileges() 
    {
        $db = Zend_Registry::get('Zend_Db');
        $select = $db->select();
        $select->from(array('p'=>'privilege'), array('id', 'allowed', 'c', 'r', 'u', 'd'))
            ->joinLeft( array('r'=>'role'), 'r.id = p.src', array('role'=>'name') )
            ->joinLeft( array('rs' => 'resource'), 'rs.id = p.dst', array('resource'=>'name') )
            ->order('p.id');
        $rows =  $select->query()->fetchAll();
        
        $data = array();
        foreach ( $rows as $i => $row ) {
            $rights = array();
            if ( $row['c'] ) $rights[] = 'create';
            if ( $row['r'] ) $rights[] = 'index';
            if ( $row['u'] ) $rights[] = 'edit';
            if ( $row['d'] ) $rights[] = 'delete';
            $item = array (
                'id' => $row['id'],
                'role' => $row['role'],
                'resource' => $row['resource'],
                'allowed' => $row['allowed'],
                'rights' => $rights
            );
            $data[] = $item;
        }
        unset($rows);

        return $data;
    }
}